package com.example.root.xposd_hook_new;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import android.telecom.PhoneAccountHandle;
import android.util.Log;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class HookTest2 implements IXposedHookLoadPackage {

    public static final String SHARK = "SharkChilli";

    private void hook_method(String className, ClassLoader classLoader, String methodName,
                             Object... parameterTypesAndCallback){
        try {
            XposedHelpers.findAndHookMethod(className, classLoader, methodName, parameterTypesAndCallback);
        } catch (Exception e) {
            XposedBridge.log(e);
        }
    }

    private void hook_methods(String className, String methodName, XC_MethodHook xmh){
        try {
            Class<?> clazz = Class.forName(className);
            for (Method method : clazz.getDeclaredMethods())
                if (method.getName().equals(methodName)
                        && !Modifier.isAbstract(method.getModifiers())
                        && Modifier.isPublic(method.getModifiers())) {
                    XposedBridge.hookMethod(method, xmh);
                }
        } catch (Exception e) {
            XposedBridge.log(e);
        }
    }

    @Override
    public void handleLoadPackage(final LoadPackageParam lpp) throws Throwable{

        Log.i(SHARK, "pkg:"+lpp.packageName);

        //getDeviceId
        hook_method("android.telephony.TelephonyManager", lpp.classLoader, "getVoicemailRingtoneUri", PhoneAccountHandle.class,new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("Rachel_xposed", "beforeHookedMethod...");
                super.beforeHookedMethod(param);
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                Log.i(SHARK, "hook getDeviceId...");
                Object obj = param.getResult();
                Log.i(SHARK, "imei args:"+obj);
                param.setResult("shark chilli");
            }
        });
    }
}
